<?php
namespace app\admin\controller;
use app\admin\logic\SpaceRemarkLogic;

/**
 * 沟通备注api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/25
 * Time: 15:47
 */
class SpaceRemark extends Base {

    public function __construct(){
        parent::__construct();
        $this->sr_logic = new SpaceRemarkLogic();
    }

    /**
     * 获取沟通备注
     * @return \think\response\Json
     */
    public function ajax_get_sr(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取沟通备注
        $r = $this->sr_logic->get_sr($params);

        $data['list'] = $r['data']['list'];
        $data['total_num'] = $r['data']['total_num'];
        return json($this->sr_logic->get_response(1,'OK',$data));
    }

    /**
     * 导出沟通备注
     * http://127.0.0.1/mandiao-mini-api/public/admin-space_remark-export_sr?token=e916a6039bff505bacaf5def2bb8435f&branchid=-1&floorid=-1&number=&work_type=-1&username=&start_time=&end_time=
     * @return \think\response\Json
     */
    public function export_sr(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取积分统计
        $r = $this->sr_logic->get_sr($params);
        $list = $r['data']['list'];

        $row = ['分部','楼层','编号','办公类型','填写人','填写时间','填写内容'];

        $export_data = [];
        foreach($list as $k => $v){
            $export_data[$k]['branch_zw'] = $v['branch_zw'];
            $export_data[$k]['floor_zw'] = $v['floor_zw'];
            $export_data[$k]['number'] = $v['number'];
            $export_data[$k]['work_type_zw'] = $v['work_type_zw'];
            $export_data[$k]['username'] = $v['username'];
            $export_data[$k]['c_time'] = $v['c_time'];
            $export_data[$k]['remark'] = $v['remark'];
        }
        array_unshift($export_data,$row);

        $file_name = '备注汇编' . date('YmdHi') . '.xlsx';
        $r = $this->sr_logic->gen_sr_excel($file_name,$export_data);

        //$this->redirect(\think\facade\Request::root(true) . '/' . $file_name);
        return json($this->sr_logic->get_response(1,'导出成功'));
    }
}